const vscode = require("vscode");
const { colors } = require("../webview/colors/colors");
function registerColorBlockDecoration(context) {
  // 定义两种装饰器类型
  const colorBlockDecoration = vscode.window.createTextEditorDecorationType({
    before: {
      contentText: " ",
      width: "10x",
      height: "10x",
      margin: "0 2px 0 0",
    },
    rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
  });

  // 更新装饰器
  const updateDecorations = (editor) => {
    if (!editor) {
      return;
    }

    const text = editor.document.getText();
    const colorRanges = [];
    const rules = [
      [/color=(\d+)/gi, [1]],
      [/SetCustomItemTextColor\s+\S+\s+(\d+)/gi, [1]],
      [/SENDMSG\s+\S+\s+\S+\s+(\d+)/gi, [1]],
      [/SENDMSG\s+\S+\s+\S+\s+\d+\s+(\d+)/gi, [1]],
      // [/AUTOCOLOR=(\d+)(,(\d+))*/gi, [1]]
    ]
    rules.forEach(rule => {
      let match;
      /s/.exec();
      /s/.test();
      '/s/'.matchAll();
      while ((match = rule[0].exec(text)) !== null) {
        rule[1].forEach(index => {
          const colorNumber = match[index];
          const color = colors.find((color) => color.number === colorNumber);
          const colorValue = color?.hex;
          if (colorValue === void 0) {
            return;
          }
          const l0 = match[0].length;
          const l1 = match[index].length;
          const startPos = editor.document.positionAt(match.index + l0 - l1);
          // 色块装饰器
          colorRanges.push({
            range: new vscode.Range(startPos, startPos),
            renderOptions: {
              before: {
                contentText: "■",
                color: colorValue,
              },
            },
          });
        });
      }
    });

    // // 颜色匹配正则（支持 HEX/RGB/HSL）
    // const COLOR_REGEX = /ccdd=(\d+)/gi;
    // let match;
    // console.log("text----------------:", text);
    // while ((match = COLOR_REGEX.exec(text)) !== null) {
    //   console.log("match--------------:", match);

    //   const colorNumber = match[1];
    //   const color = colors.find((color) => color.number === colorNumber);

    //   const colorValue = color?.hex;
    //   if (colorValue === void 0) {
    //     return;
    //   }
    //   const l0 = match[0].length;
    //   const l1 = match[1].length;

    //   const startPos = editor.document.positionAt(match.index + l0 - l1);

    //   // 色块装饰器
    //   colorRanges.push({
    //     range: new vscode.Range(startPos, startPos),
    //     renderOptions: {
    //       before: {
    //         contentText: "■",
    //         color: colorValue,
    //       },
    //     },
    //   });
    // }

    editor.setDecorations(colorBlockDecoration, colorRanges);
  };

  // 初始化及事件监听
  let activeEditor = vscode.window.activeTextEditor;
  if (activeEditor) {
    updateDecorations(activeEditor);
  }

  context.subscriptions.push(
    vscode.window.onDidChangeActiveTextEditor((editor) => {
      activeEditor = editor;
      if (editor) {
        updateDecorations(editor);
      }
    }),
    vscode.workspace.onDidChangeTextDocument((event) => {
      if (activeEditor && event.document === activeEditor.document) {
        updateDecorations(activeEditor);
      }
    })
  );
}


module.exports = { registerColorBlockDecoration };